import pygame
from openai import OpenAI

class NPC:
    def __init__(self, window, position):
        self.window = window
        self.position = position
        self.font = pygame.font.SysFont(None, 24)
        self.fontcolor = (255, 255, 255)
        self.dialogue_active = False
        self.npc_image = pygame.image.load('assets/npc.png')  # 加载NPC的图像
        self.client = OpenAI(
            base_url=f'http://10.15.88.73::5002/v1',
            api_key='ollama'
        )
        self.dialogue_history = []  # 用于存储对话历史
        self.dialogue_options = [
            "你好，我想接受任务。",
            "请告诉我关于魔法石的更多信息。",
            "我该如何提升自己的实力？",
            "有哪些怪物需要我去斩杀？"
        ]

    def render_npc(self):
        self.window.blit(self.npc_image, (self.position.x, self.position.y))  # 绘制NPC的图像

    def activate_dialogue(self):
        self.dialogue_active = True

    def get_response(self, prompt):
        # 添加上下文或指令以引导模型生成特定内容的回应
        context = (
            "你要对玩家所提出问题展开回应。"
            "玩家的任务是提升自己的实力，斩杀怪物，找回丢失的魔法石。"
            "魔法石在一千年前被哥布林军团盗走，玩家需要击败首领才能夺回魔法石，魔法石现在在首领的宝库中。"
            "斩杀哥布林会掉落金币，玩家可以向商人购买生命药水，魔力药水与武器回复自身状态并提升自己的攻击力。"
            "玩家需要面对的魔物有哥布林战士，哥布林投矛等一系列哥布林魔物，还有哥布林军团的首领。"
        )
        messages = [
            {'role': 'system', 'content': context},
            {'role': 'user', 'content': prompt}
        ]
        response = self.client.chat.completions.create(
            messages=messages,
            model='llama3.2'
        )
        return response.choices[0].message.content.strip()

    def draw_dialogue(self, player_input):
        if self.dialogue_active:
            response = self.get_response(player_input)
            self.dialogue_history.append(("玩家", player_input))
            self.dialogue_history.append(("任务发布NPC", response))
            self.display_dialogue()

    def display_dialogue(self):
        # 绘制对话框背景
        dialogue_box_rect = pygame.Rect(50, 400, 700, 150)
        pygame.draw.rect(self.window, (0, 0, 0), dialogue_box_rect)
        pygame.draw.rect(self.window, (255, 255, 255), dialogue_box_rect, 2)

        # 绘制对话内容
        y_offset = 410
        for speaker, text in self.dialogue_history[-5:]:  # 只显示最近的5条对话
            dialogue_text = self.font.render(f"{speaker}: {text}", True, self.fontcolor)
            self.window.blit(dialogue_text, (60, y_offset))
            y_offset += 30

        # 绘制对话选项
        option_y_offset = 410
        for i, option in enumerate(self.dialogue_options):
            option_text = self.font.render(f"{i + 1}. {option}", True, self.fontcolor)
            self.window.blit(option_text, (60, option_y_offset + 150))
            option_y_offset += 30

        pygame.display.flip()

    def handle_input(self, event):
        # 处理玩家输入
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
                self.draw_dialogue(self.dialogue_options[0])
            elif event.key == pygame.K_2:
                self.draw_dialogue(self.dialogue_options[1])
            elif event.key == pygame.K_3:
                self.draw_dialogue(self.dialogue_options[2])
            elif event.key == pygame.K_4:
                self.draw_dialogue(self.dialogue_options[3])
